package com.changgou.web.service.impl;

import com.changgou.web.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-07-30 22:27
 **/
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //从Cookie 中获取Jti
    public String getJtiFromCookie(ServerHttpRequest request) {
        HttpCookie httpCookie = request.getCookies().getFirst("uid");
        if (httpCookie == null) {
            return null;
        }
        return httpCookie.getValue();
    }

    //从redis中获取令牌
    public String getJwtFromRedis(String jti) {
        String jwt = stringRedisTemplate.boundValueOps(jti).get();
        if (jwt == null) {
            return null;
        }
        return jwt;
    }
}
